//
//  MakeStep1Layer.cpp
//  slushy
//
//  Created by wusonglin on 17/6/15.
//
//

#include "MakeStep1Layer.h"

IMPLEMENT_COCOS2DX_CLASS(MakeStep1Layer);
REGIST_SECENE(MakeStep1Layer)
bool MakeStep1Layer::init()
{
    if(BaseStepLayer::init()) {
        autoShowBtnName.clear();
        
        m_bIsTouch = false;
        
        _operate = StudioLayer::create("MakeStep1Layer.csb");
        this->addChild(_operate,1);
        
        auto hideNodes = {"blueberry","cola","strawberry","lemonade","grape","citrus","lime", "choosesoda"};
        for(auto name:hideNodes){
             _operate->getNodeByName(name)->setVisible(false);
            
        }
        
        
        
        return true;
    }
    return false;
}
void MakeStep1Layer::touchEnd(ui::Widget* widget)
{
    if(m_bIsTouch){
        m_bIsTouch = false;
        string nameTouch = widget->getName();
        
        auto hideNodes = {"blueberry","cola","strawberry","lemonade","grape","citrus","lime", "choosesoda"};
        
        int index = 0;
        for(auto name:hideNodes){
            if(nameTouch.compare(name) != 0){
                index++;
                
                _operate->getNodeByName(name)->runAction(Sequence::create(FadeOut::create(0.5),CallFunc::create([=](){
                
                    if(index == 7){
                        
                        auto pos = widget->getParent()->convertToWorldSpace(Vec2(320, 480));
                        
                        auto scale = ScaleTo::create(1.0, 1.5);
                        auto roat = JumpTo::create(1.0, pos, 150, 1);
                        auto spaw = Spawn::create(roat, scale, NULL);
                        
                        widget->runAction(spaw);
                        widget->setEnabled(false);
                    
                    }
                
                }), NULL));
            }
        }
    }
        
    
    
}
void MakeStep1Layer::onEnterTransitionDidFinish()
{
    BaseStepLayer::onEnterTransitionDidFinish();
    
    
    ActionHelper::delayFunc(2.0, this, [=](){
    
        
        this->showUI();
    });


}

void MakeStep1Layer::showUI()
{
    auto hideNodes1 = {"cola","strawberry","grape"};
    
    auto hideNodes2 = {"blueberry","citrus","lime"};
    
    int index = 1;
    for(auto name : hideNodes1){
        auto btn = _operate->getNodeByName(name);
        auto pos = btn->getPosition();
        btn->setPositionX(pos.x - 500);
        btn->setVisible(true);
        
        auto move = EaseElasticOut::create(MoveTo::create(1.0,pos), 1.0);
        auto roat = RotateBy::create(1.0, 360);
        auto spaw = Spawn::create(move, roat, NULL);
        btn->runAction(Sequence::create(DelayTime::create(0.5 * index ),spaw, CallFunc::create([=](){
            
            
        }), nullptr));
        index++;
    }
    
    int index2 = 1;
    for(auto name : hideNodes2){
        auto btn = _operate->getNodeByName(name);
        auto pos = btn->getPosition();
        btn->setPositionX(pos.x + 500);
        btn->setVisible(true);
        
        
        auto move = EaseElasticOut::create(MoveTo::create(1.0, pos), 1.0);
        auto roat = RotateBy::create(1.0, 360);
        auto spaw = Spawn::create(move, roat, NULL);
        btn->runAction(Sequence::create(DelayTime::create(0.5 * index2), spaw, CallFunc::create([=](){
            
            m_bIsTouch = true;
            
        }), nullptr));
        index2++;
    }
    
    auto btn = _operate->getNodeByName("lemonade");
    btn->setScale(0);
    btn->setVisible(true);
    
    auto scale = ScaleTo::create(1.0, 1.0);
    auto roat = RotateBy::create(1.0, -360);
    auto spaw = Spawn::create(roat, scale, NULL);
    btn->runAction(Sequence::create(DelayTime::create(1.0),spaw, CallFunc::create([=](){
        
    }), nullptr));
    
    
    auto font = _operate->getNodeByName("choosesoda");
    auto fontPos = font->getPosition();
    font->setPositionY(fontPos.y + 500);
    font->setVisible(true);
    auto move = EaseElasticOut::create(MoveTo::create(0.5, fontPos), 1.0);
    font->runAction(Sequence::create(DelayTime::create(1.5), move, CallFunc::create([=](){
        
    }), nullptr));

    
}




